<template>
  <a-card :loading="loading" :bordered="false">
    <!-- 业务表单 -->
    <a-form-model ref="form" :model="formData" :rules="formRules" :form="form" style="margin-top: 8px">
      <!-- 社团名称 -->
      <a-form-model-item prop="name" :labelCol="{ span: 5 }" :wrapperCol="{ span: 15 }" label="社团名称">
        <a-input
          placeholder="请输入"
          v-model="formData.name"
          :disabled="!editable || editableFiexd['name']"
        />
      </a-form-model-item>

      <!-- 所属部门 -->
      <a-form-model-item prop="sysOrgCode" :labelCol="{ span: 5 }" :wrapperCol="{ span: 15 }" label="社团所属部门">
        <j-select-depart
          v-model="formData.sysOrgCode"
          :disabled="!editable || editableFiexd['sysOrgCode']"
          @selected="setDepart"
        />
      </a-form-model-item>

      <!-- 所属负责人 -->
      <a-form-model-item prop="responsibleManId" :labelCol="{ span: 5 }" :wrapperCol="{ span: 15 }" label="社团负责人">
        <x-select-user-by-dep
          v-model="formData.responsibleManId"
          :multi="false"
          :disabled="isFlow || !editable || editableFiexd['responsibleManId']"
          @selected="setResponsibleMan"
        ></x-select-user-by-dep>
      </a-form-model-item>

      <!-- 上传图片 -->
      <a-form-model-item prop="pics" label="图片" :labelCol="{ span: 5 }" :wrapperCol="{ span: 15 }">
        <j-image-upload
          text="上传图片"
          bizPath="society"
          v-model="formData.pics"
          :disabled="!editable || editableFiexd['pics']"
          :isMultiple="true"
        ></j-image-upload>
      </a-form-model-item>

      <!-- 上传附件 -->
      <a-form-model-item prop="enclosures" label="附件" :labelCol="{ span: 5 }" :wrapperCol="{ span: 15 }">
        <j-upload :disabled="!editable || editableFiexd['enclosures']" v-model="formData.enclosures" text="上传文件" bizPath="society"></j-upload>
      </a-form-model-item>

      <!-- 指导老师 -->
      <a-form-model-item prop="instructorId" :labelCol="{ span: 5 }" :wrapperCol="{ span: 15 }" label="指导老师">
        <x-select-user-by-dep
          v-model="formData.instructorId"
          :disabled="!editable || editableFiexd['instructorId']"
          :multi="false"
          @selected="setInstructor"
        ></x-select-user-by-dep>
      </a-form-model-item>
      <!-- 成立时间 -->
      <a-form-model-item prop="buildTime" v-if="showBuildTime" label="成立时间" :labelCol="{ span: 5 }" :wrapperCol="{ span: 15 }">
        <j-date
          placeholder="请选择日期"
          :disabled="!editable"
          v-model="formData.buildTime"
          style="width: 100%"
        />
      </a-form-model-item>
      <!-- 解散时间 -->
      <a-form-model-item prop="dismissTime" v-if="canDismiss" label="解散时间" :labelCol="{ span: 5 }" :wrapperCol="{ span: 15 }">
        <j-date
          placeholder="请选择解散时间！"
          :disabled="!editable"
          v-model="formData.dismissTime"
          style="width: 100%"
        />
      </a-form-model-item>
      <slot></slot>
    </a-form-model>
  </a-card>
</template>

<script>
import JDate from '@/components/jeecg/JDate.vue'
import JSelectDepart from '@/components/jeecgbiz/JSelectDepart'
import JSelectMultiUser from '@/components/jeecgbiz/JSelectMultiUser'
import XSelectUserByDep from '@/components/extends/XSelectUserByDep'
import JDictSelectTag from '@/components/dict/JDictSelectTag.vue'
import JImageUpload from '@/components/jeecg/JImageUpload'
import JUpload from '@/components/jeecg/JUpload'
import { getSocietyById } from '@/api/api'
import { httpAction, getAction } from '@/api/manage'
import pick from 'lodash.pick'
let formData = {
  createTime: '',
  creatorId: '',
  creatorName: '',
  id: '',
  name: '',
  buildTime: '',
  dismissTime: '',
  responsibleMan: '',
  sysOrgCode: '',
  departName: ''
}
export default {
  name: 'Society',
  components: {
    JImageUpload,
    JUpload,
    JDate,
    JSelectDepart,
    JDictSelectTag,
    JSelectMultiUser,
    XSelectUserByDep
  },
  model: {
    prop: 'data',
    event: 'onchange'
  },
  props: {
    isFlow: Boolean,
    dataId: String,
    data: {
      type: Object,
      default: () => {
        return {}
      }
    },
    showBuildTime: Boolean,
    canDismiss: {
      type: Boolean,
      default: false
    },
    editable: {
      type: Boolean,
      default: true
    },
    editableFiexd: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data() {
    return {
      loading: true, //数据加载
      userDisabled: false, //用户可编辑
      url: {
        add: '/org/society/add',
        edit: '/org/society/edit',
        queryById: '/org/society/queryById'
      },
      form: this.$form.createForm(this), //表单
      formData: formData, //数据
      formRules:{
        name:[{ required: true, message: '请输入社团名称！' }],
        sysOrgCode: [{ required: true, message: '请选择所属部门！' }],
        responsibleManId:[{ required: true, message: '请选择负责人！' }],
        instructorId:[{ required: true, message: '请选择指导老师！' }]
      },
      imageList: [],
      fileList: []
    }
  },
  methods: {
    queryData(id) {
      this.loading = true
      let params = { id }
      return getAction(this.url.queryById, params).then(res => {
        if (res.success) {
          this.formData = Object.assign({}, this.formData, res.result)
        }
        return res
      }).finally(()=>{
        this.loading = false
      })
    },
    add(data) {
      this.userDisabled = true
      this.open(data)
    },
    apply(data) {
      this.userDisabled = true
      this.open(
        Object.assign({}, data, {
          workflowModel: {
            operation: 'start'
          }
        })
      )
    },
    edit(record) {
      this.userDisabled = true
      this.open(record)
    },
    open(record) {
      this.form.resetFields()
      this.loading = false
      if (record.id) {
        this.queryData(record.id).then(()=>{
          if(record.workflowModel) Object.assign(this.formData,{workflowModel:record.workflowModel})
        })
      }else{
        this.formData = Object.assign({}, record)
      }
    },
    submitForm() {
      const that = this
      return new Promise((resolve, reject) => {
        // 触发表单验证
        that.$refs.form.validate((valid, fields) => {
          if (valid) {
            let httpurl = ''
            let method = ''
            if (!that.formData.id) {
              httpurl += that.url.add
              method = 'post'
            } else {
              httpurl += that.url.add
              method = 'post'
            }
            let formData = Object.assign({},that.formData)

            httpAction(httpurl, formData, method)
              .then(res => {
                if (res.success) {
                  that.$message.success(res.message)
                  that.$emit('ok')
                  resolve(res)
                } else {
                  that.$message.warning(res.message)
                  reject(res.message)
                }
              })
              .finally(() => {
                that.loading = false
              })
          }else{
            that.$message.warning('请检查表单信息')
          }
        })
      })
    },
    setDepart(data) {
      this.formData.departName = data.name
      // this.form.getFieldDecorator('departName', {initialValue:data.name})
    },
    setResponsibleMan(data) {
      this.formData.responsibleMan = data.name
    },
    setInstructor(data) {
      this.formData.instructor = data.name
    }
  },
  watch: {
    dataId: {
      handler(nv, ov) {
        if (nv&&nv!=ov) {
          this.formData = {}
          this.queryData(nv)
        }
      },
      immediate: true
    },
    data: {
      handler(nv, ov) {
        // Object.assign(this.formData, nv)
        this.formData = nv
      },
      immediate: true,
      deep: true
    },
    formData: {
      handler(nv, ov) {
        if(nv!=ov) this.$emit('onchange', nv)
      },
      immediate: true,
      deep: true
    }
  }
}
</script>

<style></style>
